package com.qdrtme.landemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.orhanobut.logger.Logger;
import com.qdrtme.landemo.newcms.AddCommentTipoffApi;
import com.qdrtme.landemo.newcms.AddContestanApi;
import com.qdrtme.landemo.newcms.AddTipoffApi;
import com.qdrtme.landemo.newcms.AddVoteApi;
import com.qdrtme.landemo.newcms.ContestanDetailApi;
import com.qdrtme.landemo.newcms.FileUploader;
import com.qdrtme.landemo.newcms.GetMyTipoffListApi;
import com.qdrtme.landemo.newcms.GetTipoffListApi;
import com.qdrtme.landemo.newcms.VoteContestanListApi;
import com.qdrtme.landemo.newcms.VoteDetailApi;
import com.qdrtme.landemo.root.CheckCodeActivity;
import com.qdrtme.landemo.root.GetUserInfoActivity;
import com.qdrtme.landemo.root.LoginActivity;
import com.qdrtme.landemo.root.RegisterActivity;
import com.qdrtme.landemo.root.SmsResetPasswordActivity;
import com.qdrtme.landemo.root.UploadFileActivity;
import com.qdrtme.landemo.webapi.WebAPIListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    private static int REQUEST_PERMISSION_WRITE_STORAGE = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        permissionCheck();

    }

    @OnClick({R.id.btn_1})
    public void onClickBtn1(View v) {
        startActivity(new Intent(MainActivity.this, CheckCodeActivity.class));
    }

    @OnClick({R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5,
            R.id.btn_10,
            R.id.btn_30, R.id.btn_31, R.id.btn_32, R.id.btn_33, R.id.btn_34,
            R.id.btn_1216_1, R.id.btn_1216_2, R.id.btn_1216_3, R.id.btn_1216_4, R.id.btn_1216_5})
    public void onClickTT(View v) {
        switch (v.getId()) {
//            case R.id.btn_1:    // 短信验证码
//
//                break;
            case R.id.btn_2:    // 注册
                startActivity(new Intent(MainActivity.this, RegisterActivity.class));
                break;
            case R.id.btn_3:    // 登录
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                break;
            case R.id.btn_4:    // 短信重置密码
                startActivity(new Intent(MainActivity.this, SmsResetPasswordActivity.class));
                break;
            case R.id.btn_5:    // 获取用户信息
                startActivity(new Intent(MainActivity.this, GetUserInfoActivity.class));
                break;
            case R.id.btn_10:    // Base64上传图片
                startActivity(new Intent(MainActivity.this, UploadFileActivity.class));
                break;

            case R.id.btn_30:
                addTipofftest();
                break;
            case R.id.btn_31:
                // 三星手机-图片地址
                final String imgFilePath = "/storage/emulated/0/sina/weibo/weibo/img-ece6439f54ba776022c994489ae5d087.jpg";
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        testPostFile(imgFilePath, null);
                    }
                }).start();
                break;

            case R.id.btn_32:
                getTipoffListTest();
                break;

            case R.id.btn_33:
                getMyTipoffListTest();
                break;

            case R.id.btn_34:
                addCommentTipoff();
                break;

            case R.id.btn_1216_1:
                getVoteDetail();
                break;
            case R.id.btn_1216_2:
                getVoteContestantTest();
                break;
            case R.id.btn_1216_3:
                addVoteTest();
                break;
            case R.id.btn_1216_4:
                contestanDetailTest();
                break;
            case R.id.btn_1216_5:
                addContestanTest();
                break;

        }
    }

    // 分别检查是否有对应的权限，并申请
    private void permissionCheck() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
//                ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_WRITE_STORAGE);
        }
    }

    private void addContestanTest() {
        AddContestanApi addContestanApi = new AddContestanApi(this);
        addContestanApi.setContent("测试描述");
        addContestanApi.setVoteActivityId("10000000");
        addContestanApi.setPhoneNumber("110");
        addContestanApi.setRealName("测试者");
        addContestanApi.setType("app");
        addContestanApi.doHttpPost(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }

    private void contestanDetailTest() {
        ContestanDetailApi contestanDetailApi = new ContestanDetailApi(this);
        contestanDetailApi.setVoteActivityId("10000001");
        contestanDetailApi.doHttpPost(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }

    private void addVoteTest() {
        AddVoteApi addVoteApi = new AddVoteApi(this);
        addVoteApi.setVoteActivityId("10000001");
        addVoteApi.doHttpPost(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }

    private void getVoteContestantTest() {
        VoteContestanListApi voteContestanListApi = new VoteContestanListApi(this);
        voteContestanListApi.setPage("1");
        voteContestanListApi.setRows("20");
        voteContestanListApi.setVoteActivityId("10000000");
        voteContestanListApi.doHttpPost(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }


    private void getVoteDetail() {
        VoteDetailApi voteDetailApi = new VoteDetailApi(this);
        voteDetailApi.setVoteActivityId("10000000");
        voteDetailApi.doHttpPost(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }

    private void addTipofftest() {
        AddTipoffApi addTipoffApi = new AddTipoffApi(this);
        addTipoffApi.setContent("test_my_content");
        addTipoffApi.setLocation("test_location");
        addTipoffApi.setOrganizationCompanyId("1");
        addTipoffApi.setType("1");
        addTipoffApi.setUserId("1200");
        addTipoffApi.setUserName("test_name");
        addTipoffApi.setUserPic("http://test.com");
        addTipoffApi.setPicUrl("1,2,3,4");
        addTipoffApi.doHttpPost(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });

    }

    /**
     * 输入流-上传文件
     */
    private void testPostFile(final String filepath, final Handler handler) {
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("type", "0");
//        paramsMap.put("type", "0");
        FileUploader.upload(BaseApi.UPLOAD_FILE_URL, new File(filepath), paramsMap, new FileUploader.FileUploadListener() {
            @Override
            public void onProgress(long pro, double precent) {
//                Message msgProgress = new Message();
//                msgProgress.what = 0;
//                Bundle bundleProgress = new Bundle();
//                bundleProgress.putString("path", filepath);
//                bundleProgress.putInt("progress", (int) (precent * 100));
//                msgProgress.setData(bundleProgress);
//                handler.sendMessage(msgProgress);
            }

            @Override
            public void onFinish(int code, String res, Map<String, List<String>> headers) {
                String urlPath = "";
                try {
                    JSONObject object = new JSONObject(res);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String error) {

            }
        });
    }


    private void getTipoffListTest() {
        GetTipoffListApi getTipoffListApi = new GetTipoffListApi(this);
        getTipoffListApi.setPage("1");
        getTipoffListApi.setRows("10");
        getTipoffListApi.doHttpGet(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }

    private void getMyTipoffListTest() {
        GetMyTipoffListApi getMyTipoffListApi = new GetMyTipoffListApi(this);
        getMyTipoffListApi.setPage("1");
        getMyTipoffListApi.setRows("10");
        getMyTipoffListApi.setUserId("1200");  // 1200
        getMyTipoffListApi.doHttpGet(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }

    private void addCommentTipoff() {
        AddCommentTipoffApi addCommentTipoffApin = new AddCommentTipoffApi(this);
        addCommentTipoffApin.setCommentId("36");
        addCommentTipoffApin.setContent("哈哈");
        addCommentTipoffApin.setUserId("1300");
        addCommentTipoffApin.setUserNickname("爆料的评论人");
        addCommentTipoffApin.doHttpPost(new WebAPIListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onSuccess(String result) {
                Logger.json(result);
            }

            @Override
            public void onFail(int errCode, String errMessage) {

            }
        });
    }
}
